package com.testingedu.action.background;

import java.util.ArrayList;
import java.util.List;

import com.testingedu.jdbcDao.TRoleInUserDao;
import com.testingedu.jdbcDao.TUserDao;
import com.testingedu.model.TRole;
import com.testingedu.model.TUser;

/**
 * 
 * @author lsww
 * @E-mail lsww90@Gmail.com
 * @createtime 2012-11-21 下午08:54:56
 * @version 1.0
 * @since JDK 1.6
 */
public class TRoleInUserAction extends RequestContext {

	private static final long serialVersionUID = -9146777136466760390L;
	private static TRoleInUserDao tOperateInRoleDao = new TRoleInUserDao();

	public void _list() throws Exception {
		createQueryResult();
		Integer userId = Integer.parseInt(request.getParameter("userId"));
		tOperateInRoleDao.list(queryResult, null, userId, null);
		request.setAttribute("queryResult", queryResult);
		TUser tUser = new TUserDao().info(userId);
		request.setAttribute("tUser", tUser);
		request.getRequestDispatcher("/WEB-INF/admin/jsp/riu/list.jsp")
				.forward(request, response);
	}

	public void _add() throws Exception {
		String url = request.getParameter("url");
		if ("jsp".equals(url)) {
			myRole();
			request.getRequestDispatcher("/WEB-INF/admin/jsp/riu/add.jsp")
					.forward(request, response);
		} else {
			List<Integer> roleIds = new ArrayList<Integer>();
			String[] values = request.getParameter("value").split("o");
			for (int i = 0; i < values.length; i++) {
				roleIds.add(Integer.parseInt(values[i]));
			}
			Integer userId = Integer.parseInt(request.getParameter("userId"));
			tOperateInRoleDao.add(userId, roleIds);
		}
	}

	public void _delete() throws Exception {
		String url = request.getParameter("url");
		if ("jsp".equals(url)) {
			userRole();
			request.getRequestDispatcher("/WEB-INF/admin/jsp/riu/delete.jsp")
					.forward(request, response);
		} else {
			List<Integer> roleIds = new ArrayList<Integer>();
			String[] values = request.getParameter("value").split("o");
			for (int i = 0; i < values.length; i++) {
				roleIds.add(new Integer(Integer.parseInt(values[i])));
			}
			Integer userId = Integer.parseInt(request.getParameter("userId"));
			tOperateInRoleDao.delete(userId, roleIds);
		}
	}

	private void myRole() throws Exception {
		TRole tRole = new TRole();
		createQueryResult();
		PrepareParameter.execute(request, tRole);
		Integer targetUserId = Integer.parseInt(request.getParameter("userId"));
		Integer userId = getTUserId();
		tOperateInRoleDao._myRole(queryResult, userId, targetUserId, tRole);
		TUser tUser = new TUser();
		tUser = new TUserDao().info(targetUserId);
		request.setAttribute("tUser", tUser);
		request.setAttribute("queryResult", queryResult);
	}

	private void userRole() throws Exception {
		TRole tRole = new TRole();
		createQueryResult();
		PrepareParameter.execute(request, tRole);
		Integer userId = Integer.parseInt(request.getParameter("userId"));
		tOperateInRoleDao.list(queryResult, null, userId, tRole);
		TUser tUser = new TUser();
		tUser = new TUserDao().info(userId);
		request.setAttribute("tUser", tUser);
		request.setAttribute("queryResult", queryResult);
	}

}
